﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DOAN.DO;
using DOAN.BL;

namespace DOAN.Admin
{
    public partial class Xemdanhsachdethi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["quyen"] == null)
                    Response.Redirect("LoginAdmin.aspx");
                lblLoi.Text = "";
                loadMon();
                loadDT();
            }
        }

        public void loadMon()
        {
            DataTable dt = new DataTable();
            if (Session["quyen"].ToString() == "GV")
                dt = new CT_MonhocBL().GetByUser(Session["Tendangnhap"].ToString());
            else if (Session["quyen"].ToString() == "AD")
                dt = new MonhocBL().SelectAll();
            if (dt.Rows.Count > 0)
            {
                drpMon.DataSource = dt;
                drpMon.DataTextField = "Tenmon";
                drpMon.DataValueField = "Mamonhoc";
                drpMon.DataBind();
            }
            else
            {
                drpMon.Text = "Khong co mon";
            }
        }

        public void loadDT()
        {
            DataTable dt = new DataTable();
            if (Session["quyen"].ToString() == "AD")
                dt = new DethiBL().GetByMon(drpMon.SelectedValue);
            if (Session["quyen"].ToString() == "GV")
                dt = new DethiBL().GetByMon(drpMon.SelectedValue);
            griXemDethi.DataSource = dt;
            griXemDethi.DataBind();
        }

        protected void griXemDethi_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int ma = int.Parse(e.CommandArgument.ToString());
            if (e.CommandName == "xem")
            {
                Response.Redirect("ChitietDethi.aspx?IDDT=" + ma);
            }
            if (e.CommandName == "xoa")
            {
                int re = new DethiBL().Delete(new DethiDO { Madethi = ma });
                if (re == 0)
                {
                    lblLoi.Text = "Xóa thành công!";
                    loadDT();
                }
                else
                {
                    lblLoi.Text = "Lỗi!!";
                }
            }
        }

        protected void gridXemDethi_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //string monhoc = drMonhoc1.SelectedValue;
            griXemDethi.PageIndex = e.NewPageIndex;
            this.loadDT();
        }

        protected void drpMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadDT();
        }
    }
}
